package com.zyh.service;
import com.zyh.type.CodeStatusEnum;
import com.zyh.dao.TagDao;
import com.zyh.entity.Tag;
import com.zyh.exception.BusinessException;
import com.zyh.resp.Result;
import com.zyh.utils.TransactionManager;
import java.sql.SQLException;
import java.util.List;

public class TagService {

    private final TagDao tagDao = new TagDao();

    public Result<Tag> selectById(Integer tagId) {
        if (tagId == null || tagId <= 0) {
            throw new BusinessException(CodeStatusEnum.PARAM_ERROR);
        }
        try {
            TransactionManager.beginTransaction();
            Tag tag = tagDao.selectById(tagId);
            if (tag == null) {
                throw new BusinessException(CodeStatusEnum.TAG_NOT_FOUND);
            }
            TransactionManager.commit();
            return Result.success(tag);
        } catch (SQLException e) {
            try { TransactionManager.rollback(); } catch (SQLException ex) {}
            throw new BusinessException(CodeStatusEnum.SYSTEM_ERROR);
        } finally {
            try { TransactionManager.release(); } catch (SQLException e) {}
        }
    }

    public Result<Void> add(Tag tag) {
        if (tag.getName() == null || tag.getName().trim().isEmpty()) {
            throw new BusinessException(CodeStatusEnum.PARAM_EMPTY);
        }
        try {
            TransactionManager.beginTransaction();
            tagDao.add(tag);
            TransactionManager.commit();
            return Result.success();
        } catch (SQLException e) {
            try { TransactionManager.rollback(); } catch (SQLException ex) {}
            throw new BusinessException(CodeStatusEnum.SYSTEM_ERROR);
        } finally {
            try { TransactionManager.release(); } catch (SQLException e) {}
        }
    }

    public Result<Void> deleteById(Integer tagId) {
        if (tagId == null || tagId <= 0) {
            throw new BusinessException(CodeStatusEnum.PARAM_ERROR);
        }
        try {
            TransactionManager.beginTransaction();
            if (tagDao.selectById(tagId) == null) {
                throw new BusinessException(CodeStatusEnum.TAG_NOT_FOUND);
            }
            tagDao.deleteById(tagId);
            TransactionManager.commit();
            return Result.success();
        } catch (SQLException e) {
            try { TransactionManager.rollback(); } catch (SQLException ex) {}
            throw new BusinessException(CodeStatusEnum.SYSTEM_ERROR);
        } finally {
            try { TransactionManager.release(); } catch (SQLException e) {}
        }
    }

    public Result<List<Tag>> getAll() {
        try {
            TransactionManager.beginTransaction();
            List<Tag> tags = tagDao.selectAll();
            TransactionManager.commit();
            return Result.success(tags);
        } catch (SQLException e) {
            try { TransactionManager.rollback(); } catch (SQLException ex) {}
            throw new BusinessException(CodeStatusEnum.SYSTEM_ERROR);
        } finally {
            try { TransactionManager.release(); } catch (SQLException e) {}
        }
    }

    public Result<Void> updateById(Integer tagId, String newName) {
        if (tagId == null || tagId <= 0) {
            throw new BusinessException(CodeStatusEnum.PARAM_ERROR);
        }
        if (newName == null || newName.trim().isEmpty()) {
            throw new BusinessException(CodeStatusEnum.PARAM_EMPTY);
        }
        try {
            TransactionManager.beginTransaction();
            Tag tag = tagDao.selectById(tagId);
            if (tag == null) {
                throw new BusinessException(CodeStatusEnum.TAG_NOT_FOUND);
            }
            tagDao.updateById(tagId, newName);
            TransactionManager.commit();
            return Result.success();
        } catch (SQLException e) {
            try {
                TransactionManager.rollback();
            } catch (SQLException ex) {
                System.err.println("回滚事务失败: " + ex.getMessage());
            }
            throw new BusinessException(CodeStatusEnum.SYSTEM_ERROR);
        } finally {
            try {
                TransactionManager.release();
            } catch (SQLException e) {
                System.err.println("释放数据库连接失败: " + e.getMessage());
            }
        }
    }
}
